package com.example.dnfgamercommunitybackend.service.impl;

import com.example.dnfgamercommunitybackend.dao.ManageAdminDao;
import com.example.dnfgamercommunitybackend.entity.Account;
import com.example.dnfgamercommunitybackend.entity.ResponseResult;
import com.example.dnfgamercommunitybackend.service.ManageAdminService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class ManageAdminServiceImpl implements ManageAdminService {
    @Resource
    private ManageAdminDao manageAdminDao;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Override
    public String addAdmin(Account account) {
        if (Objects.isNull(account)){
            return ResponseResult.failureStringResult("请输入管理员信息");
        }
        //检查待添加用户是否存在
        if (Objects.isNull(manageAdminDao.existAdmin(account))){
            //加密密码
            account.setPassword(passwordEncoder.encode(account.getPassword()));
            if (manageAdminDao.addAdmin(account) > 0){
                //获取管理员id
                int adminId = manageAdminDao.getAdminIdByUsername(account.getUsername());
                //添加角色表
                if (manageAdminDao.addAdminRole(adminId) > 0){
                    return ResponseResult.successStringResult("添加成功");
                }
            }
        }
        return ResponseResult.failureStringResult("添加失败");
    }

    @Override
    public String deleteAdmin(int adminId) {
        if(manageAdminDao.deleteAdmin(adminId) > 0){
            return ResponseResult.successStringResult("删除成功");
        }else {
            return ResponseResult.failureStringResult("删除失败,不存在此管理员id");
        }
    }

    @Override
    public String getAllAdmin() {
        List<Account> admins = manageAdminDao.getAllAdmin();
        if (!admins.isEmpty()){
            return ResponseResult.successStringResult("响应成功",admins);
        }else {
            return ResponseResult.failureStringResult("响应失败");
        }

    }
}
